<script setup lang="ts">
import { onMounted, ref } from "vue";
import { useRoute } from "vue-router";
import {
  getMedicineOrderPreApi,
  getAddressListApi,
  pay,
  getMedicineOrderApi,
} from "@/services/consult";
import type { OrderPre, AddressItem } from "@/types/order";
import { showConfirmDialog, showToast } from "vant";
const route = useRoute();
const Medicine = ref<OrderPre>();
const Address = ref<AddressItem[]>([]);
onMounted(async () => {
  const res = await getMedicineOrderPreApi(route.query.id as string);
  const res1 = await getAddressListApi();
  Medicine.value = res.data;
  Address.value = res1.data;
});

const orderId = ref("");
const onSubmit = async () => {
  if (!checked.value) return showToast("请勾选协议");
  const res = await getMedicineOrderApi(
    route.query.id as string,
    null,
    Address.value[0].id
  );
  orderId.value = res.data.id;
  show.value = true;
};
const checked = ref(false);

const show = ref(false);
async function onClose() {
  return await showConfirmDialog({
    title: "关闭支付",
    message: "取消支付将无法获得医生回复，医生接诊名额有限，是否确认关闭？",
    cancelButtonText: "仍要关闭",
    confirmButtonText: "继续支付",
  })
    .then(() => {
      return false;
    })
    .catch(() => {
      return true;
    });
}
// 点击立即支付
const payMethod = ref(0);
async function payClick() {
  const res = await pay(
    payMethod.value,
    orderId.value,
    "http://localhost:5173/pay/result"
  );
  location.href = res.data.payUrl;
}
</script>

<template>
  <div class="order-pay-page">
    <cp-nav-bar title="药品支付"></cp-nav-bar>
    <div v-if="Medicine && Address">
      <div class="address">
        <p>
          <van-icon name="location" color="#f00" />
          <span
            >{{ Address[0]?.province }}{{ Address[0]?.city
            }}{{ Address[0]?.county }}</span
          >
        </p>
        <p>{{ Address[0]?.addressDetail }}</p>
        <p>
          {{ Address[0]?.receiver }}
          {{
            Address[0]?.mobile.replace(
              /^(1[3-9][0-9])\d{4}(\d{4}$)/,
              "$1****$2"
            )
          }}
        </p>
      </div>
      <div class="gray" style="height: 10px; background-color: #f0f0f0"></div>
      <div class="yaofang">
        <div class="top">
          <p>优医药房 <span>优医质保 假一赔十</span></p>
        </div>
        <div class="list">
          <div class="item" v-for="item in Medicine?.medicines" :key="item.id">
            <div class="order">
              <img :src="item.avatar" alt="" />
              <div class="txt">
                <p>{{ item.name }}</p>
                <p>
                  <span>{{
                    item.prescriptionFlag === 1 ? "处方药" : "非处方药"
                  }}</span>
                  <span>{{ item.specs }}</span>
                </p>
                <p>￥{{ item.amount }}</p>
              </div>
              <p>x{{ item.quantity }}</p>
            </div>
            <div class="content">
              <p>{{ item.usageDosag }}</p>
            </div>
          </div>
        </div>
        <van-cell
          border
          title="药品金额"
          :value="`￥${Medicine?.actualPayment}`"
          size="large"
        />
        <van-cell
          border
          title="运费"
          :value="`￥${Medicine?.expressFee}`"
          size="large"
        />
        <van-cell
          border
          title="优惠券"
          :value="`-￥${Medicine?.couponDeduction}`"
          size="large"
        />
        <van-cell
          class="text"
          border
          title="实付款"
          :value="`￥${Medicine?.actualPayment}`"
          size="large"
        />
        <p class="btm">
          由于药品的特殊性，如非错发、漏发药品的情况，药品一经发出
          不得退换，请核对药品信息无误后下单。
        </p>
        <van-checkbox
          style="
            padding-bottom: 100px;
            margin: auto;
            width: 200px;
            padding-top: 20px;
          "
          v-model="checked"
          >我已同意<span style="color: var(--cp-primary)"
            >支付协议</span
          ></van-checkbox
        >
        <van-submit-bar
          :price="Medicine?.actualPayment * 100"
          button-type="primary"
          button-text="立即支付"
          @submit="onSubmit"
        />
      </div>
    </div>

    <div class="order-pay-page" v-else>
      <van-skeleton
        title
        avatar
        row="2"
        style="margin-top: 15px"
      ></van-skeleton>
      <van-skeleton title row="4" style="margin-top: 50px"></van-skeleton>
      <van-skeleton title row="4" style="margin-top: 50px"></van-skeleton>
    </div>

    <!-- 支付 -->

    <van-action-sheet
      :closeable="false"
      :before-close="onClose"
      :close-on-popstate="false"
      v-model:show="show"
      title="选择支付方式"
    >
      <div class="pay-type">
        <p class="amount">￥{{ Medicine?.payment }}</p>
        <van-cell-group>
          <van-cell @click="payMethod = 0" title="微信支付">
            <template #icon><cp-icon name="consult-wechat" /></template>
            <template #extra
              ><van-checkbox :checked="payMethod === 0"
            /></template>
          </van-cell>
          <van-cell @click="payMethod = 1" title="支付宝支付">
            <template #icon><cp-icon name="consult-alipay" /></template>
            <template #extra
              ><van-checkbox :checked="payMethod === 1"
            /></template>
          </van-cell>
        </van-cell-group>
        <div class="btn">
          <van-button type="primary" round block @click="payClick"
            >立即支付</van-button
          >
        </div>
      </div>
    </van-action-sheet>
  </div>
</template>

<style lang="scss" scoped>
.pay-type {
  .amount {
    padding: 20px;
    text-align: center;
    font-size: 16px;
    font-weight: bold;
  }
  .btn {
    padding: 15px;
  }
  .van-cell {
    align-items: center;
    .cp-icon {
      margin-right: 10px;
      font-size: 18px;
    }
    .van-checkbox :deep(.van-checkbox__icon) {
      font-size: 16px;
    }
  }
}
.order-pay-page {
  height: 100vh;
  background-color: #fff;
  .address {
    width: 100%;
    padding: 10px;
    background-color: #fff;
    p:nth-child(1) {
      color: var(--cp-text3);
    }
    p:nth-child(2) {
      font-size: 18px;
      margin: 5px 0;
    }
  }
  .yaofang {
    background-color: #fff;
    width: 100%;
    margin-top: 10px;
    padding: 10px;
    .top {
      height: 30px;
      line-height: 30px;
      border-bottom: 1px solid #f0f0f0;
      margin-bottom: 10px;
      p {
        font-size: 16px;
        span {
          color: var(--cp-text3);
          font-size: 14px;
        }
      }
    }
    .list {
      .item:last-child {
        padding-bottom: 10px;
        border-bottom: 1px solid #f0f0f0;
      }
      .item {
        margin: 10px 0;

        .content {
          background-color: #f6f7f9;
          padding: 10px;
        }
        .order {
          display: flex;
          padding-bottom: 10px;
          img {
            width: 100px;
            height: 100px;
          }

          .txt {
            flex: 1;
            p:nth-child(2) {
              margin: 5px 0;
              span:nth-child(1) {
                color: #fff;
                background-color: var(--cp-primary);
                padding: 1px 4px;
                margin-right: 5px;
              }
            }
          }
        }
      }
    }
    :deep() {
      .text {
        .van-cell__value {
          color: red;
          font-size: 18px;
        }
      }
    }
  }
}
.btm::before {
  content: "*";
  color: red;
}
.btm {
  padding: 10px;
  color: var(--cp-text3);
  font-size: 12px;
  margin-top: 10px;
}
</style>
